package http_utils.https;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Map;

import com.ebo.agora.openvcall.ui.MainActivity;
import utils.Lg;


public class HttpConnect {

	private static final int TIMEOUT = 30000;

	private static HttpConnect mInstance = null;

	public static HttpConnect getInstance() {
		if (mInstance == null) {
			synchronized (MainActivity.class) {
				if (null == mInstance) {
					mInstance = new HttpConnect();
				}
			}
		}
		return mInstance;
	}

	/**
	 * Get方法请求
	 * 
	 * @param strUrl
	 *            redirect url
	 * @return NetConnectResponseInfo
	 */
	public String getConnect(String strUrl) {
		HttpURLConnection httpConnect = null;
		String strLine = null;
		try {
			URL url = new URL(strUrl);
			httpConnect = (HttpURLConnection) url.openConnection();
			httpConnect.setConnectTimeout(TIMEOUT);
			httpConnect.setReadTimeout(TIMEOUT);
			httpConnect.setRequestMethod("GET");
			httpConnect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			httpConnect.setInstanceFollowRedirects(true);
			int responseCode = httpConnect.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream inStream = httpConnect.getInputStream();
				strLine = new String(readInputStream(inStream), "utf-8");
			}
			httpConnect.disconnect();
		} catch (SocketTimeoutException ex) {
		} catch (Exception ex) {
		} finally {
			if (httpConnect != null) {
				httpConnect.disconnect();
			}
		}
		return strLine;
	}

	/**
	 * Post 方法请求.
	 * 
	 * @param strUrl
	 *            url.
	 * @param data
	 *            Request data
	 * @return
	 * 
	 */
	public String postConnect(String strUrl, Map<String, Object> data) {
		HttpURLConnection.setFollowRedirects(false);
		HttpURLConnection httpConnect = null;
		String strLine = null;
		try {
			URL url = new URL(strUrl);
			httpConnect = (HttpURLConnection) url.openConnection();
			httpConnect.setConnectTimeout(TIMEOUT);
			httpConnect.setReadTimeout(TIMEOUT);
			httpConnect.setDoInput(true);
			httpConnect.setDoOutput(true);
			httpConnect.setRequestMethod("POST");
			httpConnect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//rsa
//			String str = " ";
//			if (data != null) {
//				JSONObject jb = new JSONObject(data);
//				Lg.d("jb.toString() = " + jb.toString());
//				str = Rsa.encryptByPublic(jb.toString());
//			} else {
//				str = Rsa.encryptByPublic(str);
//			}
//			String string = "sign=" + str;
//			Lg.d("string = " + string);
//			httpConnect.getOutputStream().write(string.getBytes("UTF-8"));

			{
				StringBuilder builder = new StringBuilder();
				int i = 0;
				for (Map.Entry<String, Object> entry : data.entrySet()) {
					if (i != 0) {
						builder.append("&");
					} else {
						i++;
					}
					builder.append(entry.getKey());
					builder.append("=");
					builder.append(entry.getValue());
				}
				httpConnect.getOutputStream().write(builder.toString().getBytes("UTF-8"));
			}

			httpConnect.getOutputStream().flush();
			httpConnect.getOutputStream().close();
			HttpURLConnection.setFollowRedirects(false);
			httpConnect.connect();
			int responseCode = httpConnect.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream inStream = httpConnect.getInputStream();
				strLine = new String(readInputStream(inStream), "UTF-8");
			}

			httpConnect.disconnect();
		} catch (SocketTimeoutException ex) {
			strLine = "timeout";
			Lg.d("网络超时！！！！！！！！！！！");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (httpConnect != null) {
				httpConnect.disconnect();
			}
		}
		//rsa
//		if (strLine != null) {
//			Lg.d("strLine  1=" + strLine);
//			try {
//				byte[] bytes = strLine.getBytes("UTF-8");
//				bytes = Base64.decode(bytes, android.util.Base64.DEFAULT);
//				bytes = Rsa.decode(bytes);
//				strLine =new String(bytes) ;
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			Lg.d("strLine  2=" + strLine);
//		}
		return strLine;
	}
	/**
	 * 
	 * @param inStream
	 *            get stream type value.
	 * @return byte type value.
	 * @throws IOException
	 * @throws Exception
	 */

	public byte[] readInputStream(InputStream inStream) throws IOException {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.flush();
		outStream.close();
		inStream.close();
		buffer = null;
		return data;
	}

}
